
import DecorationView from "./DecorationView";
import User from "../../db/User";
import TextureRenderUtils from "../../../engine/TextureRenderUtils";
import SDKManager from "../../../sdk/SDKManager";
import TextureInfo from "../../../engine/comps/TextureInfo";
import { LIVING_IMG_NAME } from "../../../public/Config";
import GlobalEvent from "../../../cfw/event/GlobalEvent";
const { ccclass, property } = cc._decorator;

@ccclass
export default class LivingRoomView extends DecorationView {

    protected types: number[] = [1, 2, 3, 4]

    @property(cc.Sprite)
    bg: cc.Sprite = null;

    start() {
        super.start();
        GlobalEvent.instance().on(TextureInfo.WRITE_FILE_FINISH, this.writeFinish, this)
        this.writeFinish();
    }

    onDestroy() {
        super.onDestroy()
        GlobalEvent.instance().off(TextureInfo.WRITE_FILE_FINISH, this.writeFinish, this)
    }

    writeFinish() {
    
        if (SDKManager.getChannel().cantUseImageDataURL()) {
            let info: TextureInfo = new TextureInfo()
            info.readFile(LIVING_IMG_NAME, () => {
                // console.log(' redFinish ',LIVING_IMG_NAME)
                if (cc.isValid(this.node)){
                    this.bg.node.scaleY = -1;
                    this.bg.spriteFrame = TextureRenderUtils.getSpriteFrameByTextureInfo(info)
                }
                   
            })
        } else {
            let dataUrl = User.instance().getImgData()
            if (dataUrl) {
                TextureRenderUtils.getSpriteFrameByDataUrl(dataUrl, (sf) => {
                    if (cc.isValid(this.node))
                        this.bg.spriteFrame = sf;
                })

            }
        }
    }
}
